package cn.iocoder.yudao.hires.service.impl;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.hires.service.ResumeTrainingService;
import cn.iocoder.yudao.hires.vo.ResumeTrainingPageReqVO;
import cn.iocoder.yudao.hires.vo.ResumeTrainingSaveReqVO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.hires.entity.ResumeTrainingDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.hires.mapper.ResumeTrainingMapper;

import javax.annotation.Resource;



import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 简历-培训经历 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ResumeTrainingServiceImpl implements ResumeTrainingService {

    @Resource
    private ResumeTrainingMapper trainingMapper;

    @Override
    public Integer createTraining(ResumeTrainingSaveReqVO createReqVO) {
        // 插入
        ResumeTrainingDO training = BeanUtils.toBean(createReqVO, ResumeTrainingDO.class);
        trainingMapper.insert(training);
        // 返回
        return training.getId();
    }

    @Override
    public void updateTraining(ResumeTrainingSaveReqVO updateReqVO) {
        // 校验存在
        validateTrainingExists(updateReqVO.getId());
        // 更新
        ResumeTrainingDO updateObj = BeanUtils.toBean(updateReqVO, ResumeTrainingDO.class);
        trainingMapper.updateById(updateObj);
    }

    @Override
    public void deleteTraining(Integer id) {
        // 校验存在
        validateTrainingExists(id);
        // 删除
        trainingMapper.deleteById(id);
    }

    private void validateTrainingExists(Integer id) {
        if (trainingMapper.selectById(id) == null) {
            throw exception(new ErrorCode(500, "简历-培训经历不存在"));
        }
    }

    @Override
    public ResumeTrainingDO getTraining(Integer id) {
        return trainingMapper.selectById(id);
    }

    @Override
    public PageResult<ResumeTrainingDO> getTrainingPage(ResumeTrainingPageReqVO pageReqVO) {
        return trainingMapper.selectPage(pageReqVO);
    }

}